package com.link2room.soundwave.facade.impl;

import com.link2room.aeriabots.entity.WxQrcodeScanDetailEntity;
import com.link2room.aeriabots.service.IScanDetailService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.*;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.rodimus.dto.param.inside.FindSalesmanBySalesmanNoPararmDto;
import com.link2room.rodimus.dto.result.base.SalesmanMappingDto;
import com.link2room.soundwave.constant.L2RPointConstant;
import com.link2room.soundwave.dto.param.inside.*;
import com.link2room.soundwave.dto.result.base.*;
import com.link2room.soundwave.entity.*;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.feign.rodimus.SalesmanMappingFeignService;
import com.link2room.soundwave.service.IYhPmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import yuheng.pms.result.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by VegetaKo on 2016/9/18.
 */
@Component("yhPmsMemberHandlerFacadeService")
public class YhPmsMemberHandlerFacadeServiceImpl extends AbstractPmsMemberHandlerFacadeServiceImpl {
    @Resource
    private IYhPmsMemberService yhPmsMemberService;
    @Resource
    private ICommonService commonService;
    @Autowired
    private SalesmanMappingFeignService salesmanMappingFeignService;
    @Resource
    private IScanDetailService scanDetailService;

    @Override
    PmsMemberInfoDto checkPMSMember(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {

        return null;
    }

    @Override
    PmsMemberInfoDto bindingPMSMember(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        return null;
    }

    @Override
    void unbindingPMSMember(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {

    }

    @Override
    PmsMemberPointDto getPmsMemberPoints(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        String lhotelGroupCode=memberLoginParamDto.getLhotelGroupCode();
        String l2RMemberNo=memberLoginParamDto.getL2RMemberNo();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        PmsMemberPointDto pmsMemberPointDto=null;
        if(l2RIpms2Group!=null) {
            String pmsHotelGroupCode = l2RIpms2Group.getPmsHotelGroupCode();
            L2RYhMember queryL2RYhMember=new L2RYhMember();
            queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
            queryL2RYhMember.setL2RMemberNo(l2RMemberNo);
            L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
            if(l2RYhMember!=null){
                YhMemberInfoDto yhMemberInfoDto = yhPmsMemberService.getYhPmsMemberInfo(lhotelGroupCode, pmsHotelGroupCode, l2RYhMember.getMobile());
                if(yhMemberInfoDto!=null){
                    pmsMemberPointDto=new PmsMemberPointDto();
                    pmsMemberPointDto.setCardNo(yhMemberInfoDto.getCardNo());
                    pmsMemberPointDto.setPointBalance(yhMemberInfoDto.getPoints());
                }
            }else {
                throw new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT,"l2RMemberNo:"+l2RMemberNo);
            }
        }else{
            throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
        }
        return pmsMemberPointDto;
    }

    @Override
    QueryResult<PmsCouponDto> getPmsMemberCoupons(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        String lhotelGroupCode=memberLoginParamDto.getLhotelGroupCode();
        String l2RMemberNo=memberLoginParamDto.getL2RMemberNo();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        Optional.ofNullable(l2RIpms2Group).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE));
        String pmsHotelGroupCode = l2RIpms2Group.getPmsHotelGroupCode();
        L2RYhMember queryL2RYhMember=new L2RYhMember();
        queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
        queryL2RYhMember.setL2RMemberNo(l2RMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
        try {
            Optional.ofNullable(l2RYhMember).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT,"l2RMemberNo:"+l2RMemberNo));
            QueryResult<YhCouponDto> yhCouponDtoQueryResult = yhPmsMemberService.getYhPmsMemberCoupons(lhotelGroupCode,
                    pmsHotelGroupCode,
                    l2RYhMember.getCardNo(),
                    null,
                    null,
                    null,
                    null,
                    null,
                    memberLoginParamDto.getCurrentPage(),
                    memberLoginParamDto.getPageSize());
            Optional<QueryResult<YhCouponDto>> yhCouponDtoResult=Optional.ofNullable(yhCouponDtoQueryResult);
            List<YhCouponDto> yhCouponDtoList=yhCouponDtoResult.map(yh->yh.getResultlist()).orElse(Collections.emptyList());
            List<PmsCouponDto> pmsCouponDtoList=yhCouponDtoList.stream().map(yhCouponDto -> {
                PmsCouponDto pmsCouponDto=new PmsCouponDto();
                pmsCouponDto.setCardNo(yhCouponDto.getCardNo());
                pmsCouponDto.setCouponCode(yhCouponDto.getCouponTicketsCode());
                pmsCouponDto.setCouponName(yhCouponDto.getCouponName());
                pmsCouponDto.setCouponNo(yhCouponDto.getCouponsCode());
                pmsCouponDto.setCouponType(yhCouponDto.getCouponType());
                pmsCouponDto.setPresentValue(new BigDecimal(yhCouponDto.getCouponAmount()));
                pmsCouponDto.setSta(yhCouponDto.getStatus());
                pmsCouponDto.setLhotelGroupCode(lhotelGroupCode);
                pmsCouponDto.setValidToDate(DateUtil.getNewFormatDateString(yhCouponDto.getExpirationDate()));
                pmsCouponDto.setMinConsumeAmount(new BigDecimal(yhCouponDto.getMinConsumeAmount()));
                return pmsCouponDto;
            }).collect(Collectors.toList());
            QueryResult<PmsCouponDto> pmsCouponDtoQueryResult=new QueryResult<>();
            pmsCouponDtoQueryResult.setTotalrecord(yhCouponDtoQueryResult.getTotalrecord());
            pmsCouponDtoQueryResult.setResultlist(pmsCouponDtoList);
            pmsCouponDtoQueryResult.setCurrentPage(memberLoginParamDto.getCurrentPage());
            return pmsCouponDtoQueryResult;
        }catch (Exception e){
            throw e;
        }

    }

    @Override
    void checkRegister(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {

    }

    @Override
    PmsMemberInfoDto newPMSMember(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        return null;
    }

    @Override
    void presentPoint(PointPresentUseInsideParamDto pointPresentUseInsideParamDto) throws Exception {
        if(Objects.equals(L2RPointConstant.SRC_TYPE.MALL,pointPresentUseInsideParamDto.getSrcType())&&Objects.equals(L2RPointConstant.GOOD_TYPE.PMSPACKAGE,pointPresentUseInsideParamDto.getGoodType())){
            LogUtil.logger.info("昱恒礼包不进行积分操作");
        }else {
            String lhotelGroupCode=pointPresentUseInsideParamDto.getLhotelGroupCode();
            String l2rMemberNo=pointPresentUseInsideParamDto.getL2rMemberNo();
            L2RIpms2Group queryGroup=new L2RIpms2Group();
            queryGroup.setLhotelGroupCode(lhotelGroupCode);
            L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
            if(l2RIpms2Group!=null) {
                String pmsHotelGroupCode = l2RIpms2Group.getPmsHotelGroupCode();
                L2RYhMember queryL2RYhMember = new L2RYhMember();
                queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
                queryL2RYhMember.setL2RMemberNo(l2rMemberNo);
                L2RYhMember l2RYhMember = commonService.findOneEQ(L2RYhMember.class, queryL2RYhMember);
                if (l2RYhMember != null) {
                    yhPmsMemberService.addYhPmsMemberPoints(lhotelGroupCode,
                            pmsHotelGroupCode,
                            l2RYhMember.getCardNo(),
                            pointPresentUseInsideParamDto.getPoint().intValue(),
                            pointPresentUseInsideParamDto.getRemark());
                }
            }else{
                throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
            }
        }
    }

    @Override
    void usePoint(PointPresentUseInsideParamDto pointPresentUseInsideParamDto) throws Exception {
        if(Objects.equals(L2RPointConstant.SRC_TYPE.MALL,pointPresentUseInsideParamDto.getSrcType())&&Objects.equals(L2RPointConstant.GOOD_TYPE.PMSPACKAGE,pointPresentUseInsideParamDto.getGoodType())){
             LogUtil.logger.info("昱恒礼包不进行积分操作");
        }else {
            String lhotelGroupCode=pointPresentUseInsideParamDto.getLhotelGroupCode();
            String l2rMemberNo=pointPresentUseInsideParamDto.getL2rMemberNo();
            L2RIpms2Group queryGroup=new L2RIpms2Group();
            queryGroup.setLhotelGroupCode(lhotelGroupCode);
            L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
            if(l2RIpms2Group!=null) {
                String pmsHotelGroupCode = l2RIpms2Group.getPmsHotelGroupCode();
                L2RYhMember queryL2RYhMember = new L2RYhMember();
                queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
                queryL2RYhMember.setL2RMemberNo(l2rMemberNo);
                L2RYhMember l2RYhMember = commonService.findOneEQ(L2RYhMember.class, queryL2RYhMember);
                if (l2RYhMember != null) {
                    yhPmsMemberService.useYhPmsMemberPoints(lhotelGroupCode,
                            pmsHotelGroupCode,
                            l2RYhMember.getCardNo(),
                            pointPresentUseInsideParamDto.getPoint().intValue(),
                            pointPresentUseInsideParamDto.getRemark());
                }
            }else{
                throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
            }
        }

    }

    @Override
    List<PmsCardPointDto> getPmsMemberPointsDetail(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        String lhotelGroupCode=memberLoginParamDto.getLhotelGroupCode();
        String l2RMemberNo=memberLoginParamDto.getL2RMemberNo();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        List<PmsCardPointDto> cardPointList=null;
        if(l2RIpms2Group!=null) {
            String pmsHotelGroupCode = l2RIpms2Group.getPmsHotelGroupCode();
            L2RYhMember queryL2RYhMember = new L2RYhMember();
            queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
            queryL2RYhMember.setL2RMemberNo(l2RMemberNo);
            L2RYhMember l2RYhMember = commonService.findOneEQ(L2RYhMember.class, queryL2RYhMember);
            if (l2RYhMember != null) {
                List<YhCardPointDto> yhCardPointList = yhPmsMemberService.getYhPmsMemberPointsDetail(lhotelGroupCode, pmsHotelGroupCode, l2RYhMember.getSerialNo(), l2RYhMember.getCardNo());
                if(yhCardPointList!=null&&yhCardPointList.size()>0){
                    cardPointList=new ArrayList<>();
                    for (YhCardPointDto yhCardPoint:yhCardPointList){
                        PmsCardPointDto pmsCardPointDto=new PmsCardPointDto();
                        pmsCardPointDto.setCardNo(yhCardPoint.getCardNo());
                        pmsCardPointDto.setInvalidDate(yhCardPoint.getPointsInValidDate());
                        pmsCardPointDto.setPoint(new BigDecimal(yhCardPoint.getPoints()));
                        pmsCardPointDto.setPointGenDate(yhCardPoint.getCreateTime());
                        pmsCardPointDto.setRemark(yhCardPoint.getRemark());
                        pmsCardPointDto.setShowCardNo(yhCardPoint.getCardNo());
                        pmsCardPointDto.datetimeToString();
                        cardPointList.add(pmsCardPointDto);
                    }
                }
            }
        }else{
            throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
        }
        return cardPointList;
    }

    @Override
    PmsMemberInfoDto autoMember(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        String lhotelGroupCode=memberLoginInsideParamDto.getLhotelGroupCode();
        String memberName=memberLoginInsideParamDto.getMemberName();
        String l2RMemberNo=memberLoginInsideParamDto.getL2RMemberNo();
        String mobile=memberLoginInsideParamDto.getMobile();
        String openId=memberLoginInsideParamDto.getOpenid();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);

        if(l2RIpms2Group!=null){
            String pmsHotelGroupCode=l2RIpms2Group.getPmsHotelGroupCode();
            YhMemberInfoDto yhMemberInfoDto=this._matchingRegister(lhotelGroupCode,pmsHotelGroupCode,memberName,mobile);
            if(yhMemberInfoDto!=null){
               /* if(StringUtil.isNotBlank(yhMemberInfoDto.getName())){
                    if(memberName.equals(yhMemberInfoDto.getName())){
                        this._bindOnPMSmember(lhotelGroupCode,pmsHotelGroupCode,l2RMemberNo,openId,mobile , yhMemberInfoDto);
                    }else {
                        throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_BINDMEMBER_ERROR,"姓名不一致-注册姓名："+memberName+"-昱恒系统姓名:"+yhMemberInfoDto.getName());
                    }
                }else {*/
                    this._bindOnPMSmember(lhotelGroupCode,pmsHotelGroupCode,l2RMemberNo,openId,mobile , yhMemberInfoDto);

               // }
            }else {
                this._checkL2RYuHengRegister(lhotelGroupCode, l2RMemberNo);
                String pmsSalesmanNo=this.getPmsSalesmanNo(lhotelGroupCode,l2RMemberNo);
                this._newYuHengPmsMember(lhotelGroupCode,pmsHotelGroupCode,memberName,mobile,openId,pmsSalesmanNo,l2RMemberNo);

                yhMemberInfoDto=yhPmsMemberService.getYhPmsMemberInfo(lhotelGroupCode,pmsHotelGroupCode, mobile);

                this._saveOrUpdateYhMemberInfoToL2R(lhotelGroupCode,l2RMemberNo,mobile , yhMemberInfoDto);
            }

            return ClassConverUtil.copyProperties(yhMemberInfoDto, PmsMemberInfoDto.class);
        }else{
            throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
        }
    }

    @Override
    PmsMemberInfoDto syncMember(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        String lhotelGroupCode=memberLoginInsideParamDto.getLhotelGroupCode();
        String l2RMemberNo=memberLoginInsideParamDto.getL2RMemberNo();
        String mobileParam=memberLoginInsideParamDto.getMobile();
        String memberName=memberLoginInsideParamDto.getMemberName();
        L2RAssert.stringBlank(lhotelGroupCode,"lhotelGroupCode is null");
        L2RAssert.stringBlank(l2RMemberNo,"l2RMemberNo is null");
        L2RAssert.stringBlank(mobileParam,"mobileParam is null");
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        Optional.ofNullable(l2RIpms2Group).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE));
        L2RYhMember queryL2RYhMember=new L2RYhMember();
        queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
        queryL2RYhMember.setL2RMemberNo(l2RMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
        String mobile=Optional.ofNullable(l2RYhMember).map(value->value.getMobile()).orElse(mobileParam);
        memberName=Optional.ofNullable(l2RYhMember).map(value->value.getName()).orElse(memberName);
        YhMemberInfoDto yhMemberInfoDto = yhPmsMemberService.getYhPmsMemberInfo(lhotelGroupCode, l2RIpms2Group.getPmsHotelGroupCode(), mobile);
        if (yhMemberInfoDto!=null){
            /*if(StringUtil.isNotBlank(memberName)){
                if(memberName.equals(yhMemberInfoDto.getName())){
                    Long tempId=null;
                    if(l2RYhMember!=null){
                        tempId = l2RYhMember.getId();
                    }else {
                        l2RYhMember=new L2RYhMember();
                        l2RYhMember.setLhotelGroupCode(lhotelGroupCode);
                        l2RYhMember.setL2RMemberNo(l2RMemberNo);
                    }
                    ClassConverUtil.copyProperties(yhMemberInfoDto, l2RYhMember);
                    if(tempId!=null){
                        l2RYhMember.setId(tempId);
                    }
                    l2RYhMember.setMobile(mobile);
                    commonService.saveOrUpdate(l2RYhMember);
                    PmsMemberInfoDto pmsMemberInfoDto=ClassConverUtil.copyProperties(yhMemberInfoDto, PmsMemberInfoDto.class);
                    pmsMemberInfoDto.setIsBindPms(DefaultValue.T);
                    return pmsMemberInfoDto;
                }else {
                    throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_BINDMEMBER_ERROR,"姓名不一致-注册姓名："+memberName+"-昱恒系统姓名:"+yhMemberInfoDto.getName());
                }
            }else {*/
                Long tempId=null;
                if(l2RYhMember!=null){
                    tempId = l2RYhMember.getId();
                }else {
                    l2RYhMember=new L2RYhMember();
                    l2RYhMember.setLhotelGroupCode(lhotelGroupCode);
                    l2RYhMember.setL2RMemberNo(l2RMemberNo);
                }
                ClassConverUtil.copyProperties(yhMemberInfoDto, l2RYhMember);
                if(tempId!=null){
                    l2RYhMember.setId(tempId);
                }
                l2RYhMember.setMobile(mobile);
                commonService.saveOrUpdate(l2RYhMember);
                PmsMemberInfoDto pmsMemberInfoDto=ClassConverUtil.copyProperties(yhMemberInfoDto, PmsMemberInfoDto.class);
                pmsMemberInfoDto.setIsBindPms(DefaultValue.T);
                return pmsMemberInfoDto;
            //}
        }else {
            throw new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_MATCH);
        }

    }

    @Override
    PmsCardBalanceDto getCardBalanceInfo(MemberLoginInsideParamDto memberLoginInsideParamDto) throws Exception {
        PmsMemberPointDto pmsMemberPointDto = this.getPmsMemberPoints(memberLoginInsideParamDto);
        PmsCardBalanceDto pmsCardBalanceDto=new PmsCardBalanceDto();
        pmsCardBalanceDto.setPointBalance(pmsMemberPointDto.getPointBalance());
        return pmsCardBalanceDto;
    }

    @Override
    QueryResult<PmsCouponDto> getUsableCoupons(UsableCouopnsInsideParamDto usableCouopnsInsideParamDto) throws Exception {
        String lhotelGroupCode=usableCouopnsInsideParamDto.getLhotelGroupCode();
        String lhotelCode=usableCouopnsInsideParamDto.getLhotelCode();
        String l2rRmtype=usableCouopnsInsideParamDto.getRmtype();
        String l2RMemberNo=usableCouopnsInsideParamDto.getL2RMemberNo();
        String arr=usableCouopnsInsideParamDto.getArr();
        L2RAssert.stringBlank(lhotelCode,"lhotelCode");
        L2RAssert.stringBlank(l2rRmtype,"l2rRmtype");
        L2RAssert.stringBlank(arr,"arr");
        L2RYhRmtype l2RYhRmtype = new L2RYhRmtype();
        l2RYhRmtype.setLhotelGroupCode(usableCouopnsInsideParamDto.getLhotelGroupCode()) ;
        l2RYhRmtype.setLhotelCode(usableCouopnsInsideParamDto.getLhotelCode());
        l2RYhRmtype.setL2RRmtype(usableCouopnsInsideParamDto.getRmtype() );
        l2RYhRmtype = commonService.findOneEQ(L2RYhRmtype.class,l2RYhRmtype );
        Optional.ofNullable(l2RYhRmtype).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_MAPPINGRMTYPE_NOT_EXIT));
        String pmsHotelGroupCode = l2RYhRmtype.getHotelGroupCode();
        String pmsHotelCode = l2RYhRmtype.getHotelCode();
        String pmsRmtype = l2RYhRmtype.getPmsRmtype();
        L2RYhMember queryL2RYhMember=new L2RYhMember();
        queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
        queryL2RYhMember.setL2RMemberNo(l2RMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
        Optional.ofNullable(l2RYhMember).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT,"l2RMemberNo:"+l2RMemberNo));
        QueryResult<YhCouponDto> yhCouponDtoQueryResult = yhPmsMemberService.getYhPmsMemberCoupons(lhotelGroupCode,
                pmsHotelGroupCode,
                l2RYhMember.getCardNo(),
                pmsHotelCode,
                pmsRmtype,
                DateUtil.parseDateNewFormat(arr),
                null,
                usableCouopnsInsideParamDto.getAmount(),
                usableCouopnsInsideParamDto.getCurrentPage(),
                usableCouopnsInsideParamDto.getPageSize());
        Optional<QueryResult<YhCouponDto>> yhCouponDtoResult=Optional.ofNullable(yhCouponDtoQueryResult);
        List<YhCouponDto> yhCouponDtoList=yhCouponDtoResult.map(yh->yh.getResultlist()).orElse(Collections.emptyList());
        List<PmsCouponDto> pmsCouponDtoList=yhCouponDtoList.stream().map(yhCouponDto -> {
            PmsCouponDto pmsCouponDto=new PmsCouponDto();
            pmsCouponDto.setCardNo(yhCouponDto.getCardNo());
            pmsCouponDto.setCouponCode(yhCouponDto.getCouponTicketsCode());
            pmsCouponDto.setCouponName(yhCouponDto.getCouponName());
            pmsCouponDto.setCouponNo(yhCouponDto.getCouponsCode());
            pmsCouponDto.setCouponType(yhCouponDto.getCouponType());
            pmsCouponDto.setPresentValue(new BigDecimal(yhCouponDto.getCouponAmount()));
            pmsCouponDto.setSta(yhCouponDto.getStatus());
            pmsCouponDto.setLhotelGroupCode(lhotelGroupCode);
            pmsCouponDto.setValidToDate(DateUtil.getNewFormatDateString(yhCouponDto.getExpirationDate()));
            pmsCouponDto.setMinConsumeAmount(new BigDecimal(yhCouponDto.getMinConsumeAmount()));
            return pmsCouponDto;
        }).collect(Collectors.toList());
        QueryResult<PmsCouponDto> pmsCouponDtoQueryResult=new QueryResult<>();
        pmsCouponDtoQueryResult.setTotalrecord(yhCouponDtoQueryResult.getTotalrecord());
        pmsCouponDtoQueryResult.setResultlist(pmsCouponDtoList);
        pmsCouponDtoQueryResult.setCurrentPage(usableCouopnsInsideParamDto.getCurrentPage());
        return pmsCouponDtoQueryResult;
    }

    @Override
    PmsMemberInfoDto getCurrentUserPMSMemberInfo(BaseL2rParamDto paramDto) throws Exception {
        MemberSessionDto session = ApiThreadLocal.getMemberSessionThreadLocal();
        if(DefaultValue.T.equals(session.getIsMember())){
            L2RYhMember find = new L2RYhMember();
            find.setL2RMemberNo(session.getMemberNo());
            L2RYhMember qres = commonService.findOneEQ(L2RYhMember.class, find);
            if(qres == null){
                return null;
            }else {
                return ClassConverUtil.copyProperties(qres, PmsMemberInfoDto.class);
            }
        }else {
            return null;
        }
    }

    @Override
    String onlineSaleCoupon(UsableCouopnsInsideParamDto usableCouopnsInsideParamDto) throws Exception {
        return null;
    }

    private YhMemberInfoDto _matchingRegister(String lhotelGroupCode, String pmsHotelGroupCode, String memberName, String mobile)throws Exception{
       return yhPmsMemberService.getYhPmsMemberInfo(lhotelGroupCode,pmsHotelGroupCode, mobile);
    }

    private void _checkL2RYuHengRegister(String lhotelGroupCode, String l2rMemberNo) throws Exception {
        L2RYhMember querYhmember=new L2RYhMember();
        querYhmember.setLhotelGroupCode(lhotelGroupCode);
        querYhmember.setL2RMemberNo(l2rMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,querYhmember);
        if(l2RYhMember!=null){
            throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_REGISTER_CHECK,"昱恒会员关联关系已存在");
        }
    }

    private void _bindOnPMSmember(String lhotelGroupCode,
                                  String pmsHotelGroupCode,
                                  String l2rMemberNo,
                                  String openId,
                                  String mobile,
                                  YhMemberInfoDto yhMemberInfoDto) throws Exception {
        yhPmsMemberService.bindYhPmsMemberInfo(lhotelGroupCode, pmsHotelGroupCode, yhMemberInfoDto.getSerialNo(), yhMemberInfoDto.getCardNo(), openId);

        this._saveOrUpdateYhMemberInfoToL2R(lhotelGroupCode,l2rMemberNo,mobile, yhMemberInfoDto);

    }

    private void _saveOrUpdateYhMemberInfoToL2R(String lhotelGroupCode,
                                                String l2rMemberNo,
                                                String mobile,
                                                YhMemberInfoDto yhMemberInfoDto) throws Exception {
        L2RYhMember querYhmember = new L2RYhMember();
        querYhmember.setLhotelGroupCode(lhotelGroupCode);
        querYhmember.setL2RMemberNo(l2rMemberNo);
        L2RYhMember l2RYhMember = commonService.findOneEQ(L2RYhMember.class, querYhmember);
        Long tempId=null;
        if(l2RYhMember!=null){
            tempId = l2RYhMember.getId();
        }else {
            l2RYhMember=new L2RYhMember();
            l2RYhMember.setLhotelGroupCode(lhotelGroupCode);
            l2RYhMember.setL2RMemberNo(l2rMemberNo);
        }
        ClassConverUtil.copyProperties(yhMemberInfoDto, l2RYhMember);
        if(tempId!=null){
            l2RYhMember.setId(tempId);
        }
        l2RYhMember.setMobile(mobile);
        commonService.saveOrUpdate(l2RYhMember);
    }

    /*private void _newYuHengPmsMember(String lhotelGroupCode,
                                     String pmsHotelGroupCode,
                                     String memberName,
                                     String mobile,
                                     String openId,
                                     String pmsSalesmanNo)throws Exception{
        List<YhCardNoDto> yhCardNoDtos= yhPmsMemberService.getCardsNoList(lhotelGroupCode,pmsHotelGroupCode);
        boolean isRetry=true;
        if(yhCardNoDtos!=null&&yhCardNoDtos.size()>0){
            for(YhCardNoDto yhCardNoDto:yhCardNoDtos){
                YhMemberInfoDto yhMemberInfoDto=yhPmsMemberService.newYuHengPmsMember(lhotelGroupCode,pmsHotelGroupCode,memberName,mobile,yhCardNoDto.getCardNo(),openId,pmsSalesmanNo);
                if(yhMemberInfoDto.getSuccess()){
                    isRetry=false;
                    break;
                }else{
                    if(!yhMemberInfoDto.getCode().equals(10102)){
                        throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_NEWMEMBER_ERROR,yhMemberInfoDto.getMessage());
                    }
                }
            }
        }
        if(isRetry){
            this._newYuHengPmsMember(lhotelGroupCode,pmsHotelGroupCode,memberName,mobile,openId,pmsSalesmanNo);
        }
    }*/

    private void _newYuHengPmsMember(String lhotelGroupCode,
                                     String pmsHotelGroupCode,
                                     String memberName,
                                     String mobile,
                                     String openId,
                                     String pmsSalesmanNo,
                                     String l2rMemberNo)throws Exception{
        YhCardNoDto yhCardNoDto= yhPmsMemberService.getCardNo(lhotelGroupCode,pmsHotelGroupCode);
        YhMemberInfoDto yhMemberInfoDto=yhPmsMemberService.newYuHengPmsMember(lhotelGroupCode,pmsHotelGroupCode,memberName,mobile,yhCardNoDto.getCardNo(),openId,pmsSalesmanNo, l2rMemberNo);
        if(!yhMemberInfoDto.getCode().equals(10102)&&!yhMemberInfoDto.getSuccess()){
            throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_NEWMEMBER_ERROR,yhMemberInfoDto.getCode()+yhMemberInfoDto.getMessage());
        }
    }

    @Override
    PmsMemberInfoDto upgradePmsMemberLevel(MemberUpgradeInsideParamDto memberUpgradeInsideParamDto) throws Exception {
        String lhotelGroupCode=memberUpgradeInsideParamDto.getLhotelGroupCode();
        String l2RMemberNo=memberUpgradeInsideParamDto.getL2RMemberNo();
        String mobile=memberUpgradeInsideParamDto.getMobile();
        String memberName=memberUpgradeInsideParamDto.getMemberName();
        L2RAssert.stringBlank(lhotelGroupCode,"lhotelGroupCode is null");
        L2RAssert.stringBlank(l2RMemberNo,"l2RMemberNo is null");

        L2RYhMember queryL2RYhMember=new L2RYhMember();
        queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
        queryL2RYhMember.setL2RMemberNo(l2RMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
        Optional.ofNullable(l2RYhMember).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT,"l2RMemberNo:"+l2RMemberNo));
        List<YhCardTypeRuleDto> cardTypeRuleList = yhPmsMemberService.getCardTypeRule(lhotelGroupCode, l2RYhMember.getGroupCode(),l2RYhMember.getHotelCode() , l2RYhMember.getCardLevel());
        return cardTypeRuleList.stream().filter(event -> Objects.equals(event.getFromCardType(), l2RYhMember.getCardLevel())).findAny().map(
                TryExceptionUtil.of(event -> {
                    if(Objects.equals("OR",event.getRelation())){
                        if(memberUpgradeInsideParamDto.getAmount().compareTo(event.getAmount())!=0&&
                                memberUpgradeInsideParamDto.getPoint().compareTo(event.getPoints())!=0 ){
                            throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_CARD_UPGRADE_VALUE_ERROR,
                                    "规则关系："+event.getRelation()+";规则金额："+event.getAmount()+";规则积分："+event.getPoints());
                        }
                    } else if(Objects.equals("AND",event.getRelation())){
                        if(memberUpgradeInsideParamDto.getAmount().compareTo(event.getAmount())!=0||
                                memberUpgradeInsideParamDto.getPoint().compareTo(event.getPoints())!=0 ){
                            throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_CARD_UPGRADE_VALUE_ERROR,
                                    "规则关系："+event.getRelation()+";规则金额："+event.getAmount()+";规则积分："+event.getPoints());
                        }
                    }else {
                        throw new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_CARD_UPGRADE_RELATION_MISSING,event.getRelation());
                    }
                    L2RYhMemberUpgradeDetail l2RYhMemberUpgradeDetail=ClassConverUtil.copyProperties(l2RYhMember,L2RYhMemberUpgradeDetail.class);
                    l2RYhMemberUpgradeDetail.setToCardLevel(event.getToCardType());
                    l2RYhMemberUpgradeDetail.setUseAmount(memberUpgradeInsideParamDto.getAmount());
                    l2RYhMemberUpgradeDetail.setUsePoint(memberUpgradeInsideParamDto.getPoint());
                    l2RYhMemberUpgradeDetail.setRuleAmount(event.getAmount());
                    l2RYhMemberUpgradeDetail.setRulePoint(event.getPoints());
                    l2RYhMemberUpgradeDetail.setRelation(event.getRelation());
                    l2RYhMemberUpgradeDetail.setIsSuccess(DefaultValue.F);
                    l2RYhMemberUpgradeDetail.setId(null);
                    commonService.save(l2RYhMemberUpgradeDetail);
                    String pmsSalesmanNo=this.getPmsSalesmanNo(lhotelGroupCode,l2RMemberNo);
                    yhPmsMemberService.upgradeCard(lhotelGroupCode,
                            l2RYhMember.getGroupCode(),
                            l2RYhMember.getHotelCode(),
                            l2RYhMember.getCardNo(),
                            l2RYhMember.getSerialNo(),
                            event.getFromCardType(),
                            event.getToCardType(),
                            memberUpgradeInsideParamDto.getAmount().doubleValue(),
                            memberUpgradeInsideParamDto.getPoint().intValue(),
                            pmsSalesmanNo
                    );
                    l2RYhMemberUpgradeDetail.setIsSuccess(DefaultValue.T);
                    commonService.merge(l2RYhMemberUpgradeDetail);
                    MemberLoginInsideParamDto memberLoginInsideParamDto=new MemberLoginInsideParamDto();
                    memberLoginInsideParamDto.setL2RMemberNo(l2RMemberNo);
                    memberLoginInsideParamDto.setMobile(l2RYhMember.getMobile());
                    memberLoginInsideParamDto.setMemberName(l2RYhMember.getName());
                    return this.syncMember(memberLoginInsideParamDto);
                })
        ).orElseThrow(() -> new SoundwaveException(SoundwaveSysExceptionEnum.YHPMS_GET_CARDTYPE_RULE_ERROR));
    }

    @Override
    QueryResult<PmsPackageDto> listPmsPackage(BaseParamDto baseParamDto) throws Exception {
        String lhotelGroupCode=baseParamDto.getLhotelGroupCode();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        Optional.ofNullable(l2RIpms2Group).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE));
        QueryResult<YhActivityDto> activityResult = yhPmsMemberService.listYhActivity(lhotelGroupCode, l2RIpms2Group.getPmsHotelGroupCode(), baseParamDto.getCurrentPage(), baseParamDto.getPageSize());
        QueryResult<PmsPackageDto> pmsPackageDtoQueryResult=new QueryResult<>();
        pmsPackageDtoQueryResult.setCurrentPage(baseParamDto.getCurrentPage());
        pmsPackageDtoQueryResult.setTotalrecord(activityResult.getTotalrecord());
        List<PmsPackageDto> pmsPackageDtoList=activityResult.getResultlist().stream().map(
                TryExceptionUtil.of(
                        event->{
                            PmsPackageDto pmsPackageDto=new PmsPackageDto();
                            pmsPackageDto.setLhotelGroupCode(lhotelGroupCode);
                            pmsPackageDto.setId(event.getId().longValue());
                            pmsPackageDto.setActivityCode(event.getActivityCode());
                            pmsPackageDto.setActivityName(event.getActivityName());
                            pmsPackageDto.setExchangeType(event.getExchangeType());
                            pmsPackageDto.setAmount(event.getAmount());
                            pmsPackageDto.setRemark(event.getRemark());
                            pmsPackageDto.setPoints(event.getPoints());
                            pmsPackageDto.setStartDate(event.getStartDate());
                            pmsPackageDto.setEndDate(event.getEndDate());
                            pmsPackageDto.datetimeToString();
                            return pmsPackageDto;
                        }
                )
        ).collect(Collectors.toList());
        pmsPackageDtoQueryResult.setResultlist(pmsPackageDtoList);
        return pmsPackageDtoQueryResult;
    }

    @Override
    PmsPackageDto getPmsPackage(BaseParamDto baseParamDto) throws Exception {
        L2RAssert.numberNull(baseParamDto.getId(),"id is null");
        String lhotelGroupCode=baseParamDto.getLhotelGroupCode();
        YhActivityDto yhActivityDto=yhPmsMemberService.getYhActivity(lhotelGroupCode,baseParamDto.getId().toString());
        PmsPackageDto pmsPackageDto=new PmsPackageDto();
        pmsPackageDto.setLhotelGroupCode(lhotelGroupCode);
        pmsPackageDto.setId(yhActivityDto.getId().longValue());
        pmsPackageDto.setActivityCode(yhActivityDto.getActivityCode());
        pmsPackageDto.setActivityName(yhActivityDto.getActivityName());
        pmsPackageDto.setExchangeType(yhActivityDto.getExchangeType());
        pmsPackageDto.setAmount(yhActivityDto.getAmount());
        pmsPackageDto.setRemark(yhActivityDto.getRemark());
        pmsPackageDto.setPoints(yhActivityDto.getPoints());
        return pmsPackageDto;
    }

    @Override
    void exchangePackage(MemberPackageInsideParamDto memberPackageInsideParamDto) throws Exception {
        String lhotelGroupCode=memberPackageInsideParamDto.getLhotelGroupCode();
        String l2rMemberNo=memberPackageInsideParamDto.getL2rMemberNo();
        String activityCode=memberPackageInsideParamDto.getActivityCode();
        String exchangeType=memberPackageInsideParamDto.getExchangeType();
        Double amount=Optional.ofNullable(memberPackageInsideParamDto.getAmountValue()).map(value->value.doubleValue()).orElse(null);
        Integer point=Optional.ofNullable(memberPackageInsideParamDto.getPointValue()).map(value->new BigDecimal(0).subtract(value).intValue()).orElse(null);
        if(Objects.equals("4",exchangeType)){
            amount=null;
            point=null;
        }
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        Optional.ofNullable(l2RIpms2Group).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE));
        L2RYhMember queryL2RYhMember=new L2RYhMember();
        queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
        queryL2RYhMember.setL2RMemberNo(l2rMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
        Optional.ofNullable(l2RYhMember).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT,"l2RMemberNo:"+l2rMemberNo));
        String resultInfo=yhPmsMemberService.exchangePackage(lhotelGroupCode,
                l2RIpms2Group.getPmsHotelGroupCode(),
                l2RYhMember.getCardNo(),
                activityCode,
                exchangeType,
                DateUtil.getNewFormatDateString(new Date()),
                l2RYhMember.getMobile(),
                amount,
                point
                );
        L2RYhPackageSaleDetail l2RYhPackageSaleDetail=new L2RYhPackageSaleDetail();
        l2RYhPackageSaleDetail.setLhotelGroupCode(lhotelGroupCode);
        l2RYhPackageSaleDetail.setOrderNo(memberPackageInsideParamDto.getOrderNo());
        l2RYhPackageSaleDetail.setL2RMemberNo(l2rMemberNo);
        l2RYhPackageSaleDetail.setResultInfo(resultInfo);
        commonService.save(l2RYhPackageSaleDetail);
    }

    private String getPmsSalesmanNo(String lhotelGroupCode,String l2rMemberNo) throws Exception {
        WxQrcodeScanDetailEntity wxQrcodeScanDetailEntity = scanDetailService.fansLastScanSalesman(lhotelGroupCode, l2rMemberNo);
        if(wxQrcodeScanDetailEntity!=null){
            String salesmanNo=wxQrcodeScanDetailEntity.getQrKey();
            FindSalesmanBySalesmanNoPararmDto findSalesmanBySalesmanNoPararmDto=new FindSalesmanBySalesmanNoPararmDto();
            findSalesmanBySalesmanNoPararmDto.setLhotelGroupCode(lhotelGroupCode);
            findSalesmanBySalesmanNoPararmDto.setSalesmanNo(salesmanNo);
            String pmsSalesmanNo=null;
            String pmsSalesmanName=null;
            if(StringUtil.isNotBlank(salesmanNo)){
                try {
                    BaseResultDto<SalesmanMappingDto> salesmanMappingDtoBaseResultDto = salesmanMappingFeignService.getMappingEmployee(findSalesmanBySalesmanNoPararmDto.paramToMap());
                    pmsSalesmanNo=salesmanMappingDtoBaseResultDto.getResultInfo().getEmployeeNo();
                    pmsSalesmanName=salesmanMappingDtoBaseResultDto.getResultInfo().getEmployeeName();
                }catch (Exception e){
                    LogUtil.logger.error(ExceptionUtil.getExceptionDetail(e));
                }
            }
            return pmsSalesmanNo+"^"+pmsSalesmanName;
        }else {
            return null;
        }

    }

	@Override
	public void coupenCodeCollarCoupenHandler(MemberCoupenCodeCollarCoupenInsideParamDto memberCouponsCodeInsideParamDto)
			throws Exception {
		String lhotelGroupCode=memberCouponsCodeInsideParamDto.getLhotelGroupCode();
        String l2rMemberNo=memberCouponsCodeInsideParamDto.getL2rMemberNo();
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(lhotelGroupCode);
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        Optional.ofNullable(l2RIpms2Group).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE));
        L2RYhMember queryL2RYhMember=new L2RYhMember();
        queryL2RYhMember.setLhotelGroupCode(lhotelGroupCode);
        queryL2RYhMember.setL2RMemberNo(l2rMemberNo);
        L2RYhMember l2RYhMember=commonService.findOneEQ(L2RYhMember.class,queryL2RYhMember);
        Optional.ofNullable(l2RYhMember).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.PMSMEMBER_NOT_EXIT,"l2RMemberNo:"+l2rMemberNo));
        String resultInfo=yhPmsMemberService.coupenCodeCollarCoupen(lhotelGroupCode,
                l2RIpms2Group.getPmsHotelGroupCode(),
                l2RYhMember.getCardNo(),
                memberCouponsCodeInsideParamDto.getCouponsCode(),
                memberCouponsCodeInsideParamDto.getCouponsCodeType(),
                l2RYhMember.getMobile()
                );
	}
}
